 var  rm = null;
$.ajaxSetup({
	type: "GET"
	,cache: false
	,dataType: "json"
	,contentType:"application/json; charset=ISO-8859-1"
	,url:"ws"
	,error:function(x,e,s){
		var errotxt = "Unknow Error";
		if (x.status == 0) {
			errotxt = 'You are offline!! Please Check Your Network.';
		} else if (x.status == 404) {
			errotxt = 'URL not found.';
		} else if (x.status == 500) {
			errotxt = 'Internal Server Error.';
		} else if (e == 'timeout') {
			errotxt = 'Request Time out.' ;
		} else if (e == 'parsererror') {			
			errotxt ='Error. Parsing JSON Request failed.';
		}else{
			errotxt = 'Unknow Error.' + x.responseText;
		}
		/*
		var idrequest_t = this.data.substring(this.data.indexOf("idRequest")+10);
		idrequest_t = idrequest_t.substring(0,idrequest_t.indexOf("&"));
		//alert('idRequest: '+idrequest_t+'\nerro: '+errotxt);
		var task = $("#uid_"+idrequest_t)[0]["dta"];				
		task.imgReload.htmlX.attr("src","lib/org/underas/assets/img16x16/change-icon.png");
		task.erroState(errotxt);
		*/
		rm._log_erro({"s":"RequestManager.Erro"+"","e":[errotxt]});
	}      
});

function RequestManager(){   	
	
	/*
	this.htmlX.find(".close").click(function(){
		//rm.boxTasks.htmlX.find(".terro").remove();		
	});	
	*/
	this._log_erro=function(args){};
	this._log_success=function(args){};
}

RequestManager.prototype.finalizar = function(){
    //this.show(false,false);
    //this.pbPrincipal.setProgress(0);
    //this.htmlX.find(".finalizado").remove();
};
RequestManager.prototype.addRequest = function(req){		
	req.url = req.url||"ws";
	req.format = req.format||"json";	
	var tsk = new Task(req);	
	if(typeof req.puid == "undefined"){
		var eleM = $("#conteudo .windowCA:first");
		if(eleM.length > 0){
			req.puid = 	eleM[0]["dta"]._fnName;		
		}else{
			req.puid = $("#conteudo .windowC:first")[0]["dta"]._fnName;
		}		
		//req.puid = $("#navbarlist li.active")[0]["dta"].puid);
	}	
	//tsk.htmlX.hide();
	window[req.puid].htmlX.find(".blockrequest").addClass("_req_"+tsk.idRequest).show().find(".BoxTasks").append(tsk.htmlX);
    var req2 = {"idRequest":tsk.idRequest,"t":tsk.t,"s":tsk.service};
    for(var key in req){
    	if(key!="onLoad"&&req[key]!=""&&key!="url"&&key!="puid"&&req[key]!="null"){
    		req2[key]=req[key];
    	}    	
    };    
    $.ajax({
		"url":req.url,
	    "data":req2,
		"cross-origin":true,
		"dataType":req.format,
	    "success":function(dtr){	   
			var eleM = $("#uid_"+dtr.idRequest);		
			if(dtr.erro.length==0){				
				eleM[0]["dta"].finalizar(dtr);
				var paiRE = eleM.parents(".blockrequest");				
				paiRE.removeClass("_req_"+dtr.idRequest);				
				var classes = paiRE.attr("class");				
				if(classes.indexOf("_req_") < 0){
					paiRE.hide().find(".Task").remove().end().find(".BoxTasks").hide();
				}				
			}else{
				eleM[0]["dta"].erroState(dtr.erro);
			}
	    }
    }); 
    
 };
 
RequestManager.prototype.refresh = function(){  
	/*
	var tmFinalizados = this.boxTasks.htmlX.find(".finalizado").size();
	var tmtTodos = this.boxTasks.htmlX.find(".Task").size();
	var par =  (tmFinalizados/tmtTodos)*100;
	par = parseInt(par);	
	this.pbPrincipal.setProgress(par);	
	if(par==100){
		//acabou oooooo, acabou ooooo
		rm.finalizar();
	}
	//alert(tmFinalizados+"/"+tmtTodos+"  --> "+par+"%");
	*/
 };
 
 RequestManager.prototype.showAllTasks = function(event){ 
	$(event).find(".BoxTasks").show();
 };
 
 RequestManager.prototype.hideAllTasks = function(event){ 
	if($(event).hasClass("erro_load")){
		$(event).find(".BoxTasks").hide();	
	}	
 };
 RequestManager.prototype.removeAllTasks = function(event){ 
	 	
		if($(event).hasClass("erro_load")){
			$(event).find(".Task").remove();	
			$(event).hide().removeClass().addClass("blockrequest normal_load");
		}	
		//$(event).hide();
 };
 

$(function(){
	rm = new RequestManager();
	//$("body").append(rm.htmlX); 
});
